package cn.caskm.souvenirshopsystem.config;

import cn.caskm.souvenirshopsystem.interceptors.LoginInterceptor;
import cn.hutool.core.util.BooleanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final JwtProperties jwtProperties;

    private final AuthProperties authProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 校验是否开启拦截
        if (BooleanUtil.isFalse(authProperties.getEnabled())) {
            return;
        }

        // 注册拦截器
        registry.addInterceptor(new LoginInterceptor(jwtProperties))
                .excludePathPatterns(authProperties.getExcludePaths())
                .order(Integer.MIN_VALUE);
    }
}
